<?php
declare(strict_types=1);

/**
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 * @link      https://cakephp.org CakePHP(tm) Project
 * @since     0.2.9
 * @license   https://opensource.org/licenses/mit-license.php MIT License
 */
namespace App\Controller;


use Cake\Event\Event;
use App\Job\TestJob;
use Cake\Queue\QueueManager;

/**
 * Static content controller
 *
 * This controller will render views from templates/Pages/
 *
 * @link https://book.cakephp.org/4/en/controllers/pages-controller.html
 */
class DemosController extends AppController
{
	public function initialize(): void
    {
        parent::initialize();
    }

    //event_test
    public function getEvent() {
        $this->getEventManager()->dispatch(new Event('Order.afterPlace', $this, ['data' => ['code'=> 666]]));
        $this->G->success('success', time());
    }

    //queue_test
    public function getQueue() {

        $data = ['title' => 'QueueTest', 'code' => rand(0, 1000)];
        $options = ['config' => 'default',
                'delay' => 10
            ];

        QueueManager::push(TestJob::class, $data, $options);
        $this->G->success('success', time());
    }

	//获取所有的分类
    public function index()
    {
        // Set the view vars that have to be serialized.

		$result = $this->Demos->find('treeList', [
			'keyPath' => 'id',
			'valuePath' => 'title',
			'spacer' => '-'
		]);
      $this->G->success($result);
    }

	public function view(){

	}

//	public function getTest(){
//		$data = [
//			'status' => 3,
//            'title' => 'ccc'
//		];
//		$imgCategory = $this->Demos->newEntity($data);
//		pr($imgCategory->getErrors());
//		if($imgCategory->getErrors()){
//
//			$this->G->error($imgCategory->getErrors());
//		}
//		pr($this->Demos->save($imgCategory)? 'true' : $imgCategory->getErrors());
//	}

	//新增分类
	public function add()
	{
		if(!$this->request->is("post"))
		{
			$this->G->error("post_error");
			return null;
      }
		$parent_id = $this->request->getData("parent_id");
		$title = $this->request->getData("title");
		$parent_id = is_numeric($parent_id) ? null : $parent_id;

		$imgCategory = $this->Demos->newEmptyEntity();
		$imgCategory->title = $title;
		$imgCategory->status = $status;
		$imgCategory->parent_id = $parent_id;
		if ($this->Demos->save($imgCategory)){
			$this->G->success($imgCategory->id);
		} else {
			$this->G->error('save_error');
		}

	}

		//修改分类
	public function update()
	{
		if(!$this->request->is("post"))
		{
			$this->G->error("post_error");
			return null;
      }

		$id = $this->request->getData("id");
		$parent_id = $this->request->getData("parent_id");
		$title = $this->request->getData("title");
		$status = $this->request->getData("status");
		$parent_id = is_numeric($parent_id) ? null : $parent_id;

		if(!is_numeric($id)){
			$this->G->error("id_error");
			return null;
		}

		$imgCategory = $this->Demos->get($id);
		$imgCategory->title = $title;
		$imgCategory->status = $status;
		$imgCategory->parent_id = $parent_id;
		if ($this->Demos->save($imgCategory)){
			$this->G->success($imgCategory->id);
		} else {
			$this->G->error('save_error');
		}

	}
	//删除分类
	public function delete()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
		  $imgCategory = $this->Demos->get($id);
		  $this->Demos->removeFromTree($imgCategory);
        $result = $this->Demos->delete($imgCategory);
        if($result){
            $this->G->error('delete_error');
            return null;
        }
        $this->G->success('Success');
    }

	//移动分类
	 public function move()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        $action = $this->request->getData("action");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        if(!in_array($action, ['up','down'])){
            $this->G->error("action_error");
            return null;
        }
		  $imgCategory = $this->Demos->get($id);
		  if ($action == 'up'){
			  $result = $this->Demos->moveUp($imgCategory);
		  } else {
			  $result = $this->Demos->moveDown($imgCategory);
		  }

        if(is_string($result)){
            $this->G->error($result);
            return null;
        }
        $this->G->success('Success', $result);
    }

}
